<template>
    <div class='commonListComplex' v-loading="loading">
       
        <div class='commonList_title' v-if="isTitle=='Y'" ><i class='fa fa-info-circle'></i>{{title}}</div>
        <div class='commonList_title commonList_title_center' v-if="isApp=='Y'&&isAppTop==''" ><i class='el-icon-arrow-left' @click="returns" style='margin-right:10px;float:left;line-height:30px'>返回</i><i class='fa fa-info-circle'></i>{{title}}</div>
       <div class='commonList_title commonList_title_center2' v-if="isApp=='Y'&&isAppTop=='Y'" ><i class='el-icon-arrow-left' @click="returns" style='margin-right:10px;float:left;line-height:30px'>返回</i><i class='fa fa-info-circle'></i>{{title}}</div>
       
        <search @search-event="searchEvent" :searchStr="searchStr"  ref="searchs" v-if="searchStr.length>0"></search>
         <el-row class='elrows2' v-if="buttonJson.length>0" style='background:#fff'>
            <el-col :span="24" style='padding:0'>
                <button-bar :buttonJson="buttonJson" @btn-click="btnClick"></button-bar>
            </el-col>
            
        </el-row>
        
        <div class='list' >
           
            <el-row v-for="count in row" :key="count" class='listComplex_row' >
                <el-col :span="parseInt(lists[(count-1)*col+count2-1].width)" v-for="count2 in col" :key="count2" class='listComplex_col' style="height:100%;">
                    <commonList :noclick="noclick" :index="(count-1)*col+count2-1" :model="model" ref="commonList" @loadDatad="loadDatad"  isTitle='Y' @openWin="openWin" :company="company"  listnames=""  @rowDbClick="rowDbClick"  :model_key="lists[(count-1)*col+count2-1].modelKey" :sKey2="lists[(count-1)*col+count2-1].sKey" hidenSearch='Y' :searchJson="searchStr2" sKey=""  searchs=""  @rowClick="rowClick" @rowMultiClick="rowMultiClick" :appid="appid" style="border-right:1px solid #ddd;border-left:1px solid #ddd"></commonList>
                </el-col>
            </el-row>
            
           
        </div>
         
        <common-print :printDatas="printData" style='height:0'></common-print>
    </div>
</template>
<script>
import commonList  from './commonList.vue'
import ButtonBar  from './ButtonBar.vue'
import search  from './search.vue'
import CommonPrint  from '../../views/common/print/CommonPrint.vue'
export default {
    name:'commonListComplex',
    props:['isApp','sKey','sOtherKey','isTitle','row','company','sKey2','appid','listname','exkey','width','filenameChild','button','fieldPath','model_key','model'],
    components: {
        commonList,
        ButtonBar,
        search,
        CommonPrint
    },
    data(){
        return{
            printData:{},
            lists:[],
            searchs:'',
            buttonJson:[],
            col:0,
            searchStr:[],
            title:"",
            searchStr2:[],
            loading:true,
            curRow:null,//不管哪个table点都替换
            curRows:null,
            curRowComplex:[],//每个table都记录
            curRowsComplex:[],
            noclick:"N",
            listnames:"",
            showList:true,
            isAppTop:localStorage.getItem("isAppTop")
        }
    },
    created(){

        this.filepaths="";
        this.loadData();
    }, 
    mounted(){
        if(this.row>1){
            let el=document.getElementsByClassName("listComplex_row");
            let height=document.getElementsByClassName("list")[0].offsetHeight;
            for(let i=0;i<el.length;i++){
              //  el[i].style.height=(height/2-40)+'px';
                el[i].style.flexDirection= "column";

            }
        }else{
            let el=document.getElementsByClassName("listComplex_row");
            let height=document.getElementsByClassName("list")[0].offsetHeight;
            for(let i=0;i<el.length;i++){
                //el[i].style.height=(height)+'px';
                el[i].style.flexDirection= "row";
                let el_=el[i].getElementsByClassName("el-col");
                for(let h=0;h<el_.length;h++){
                    el_[h].style.paddingRight="5px"
                }
            }
            
            
        }
    },
    methods:{
        loadData(isDevpos){
            this.showList=false;
            this.$post(this.appid,"api/view/commonListComplexJson",{
                sKey:this.sKey,
                sKey2:this.sKey2,
                sOtherKey:this.sOtherKey,
                listname:escape(this.listname),
                key:escape(this.model_key),
                model:escape(this.model)
            }).then(data=>{
                this.lists=data.list; 
                this.buttonJson=data.buttonJson;
                this.col=Number(data.col);
                this.searchStr=data.searchJson;
                this.searchStr2=data.searchJson;
                this.loading=false;
                this.listnames=data.listname;
                this.title=data.name;
                this.$emit("loadDataMaind",this.listnames)
                this.showList=true;
                console.log(this.lists,"this.lists=====================================")

                if(isDevpos=="Y"){
                    try{
                        let emls=this.$refs.commonList;
                        for(let i=0;i<emls.length;i++){
                            emls[i].loadData("Y")
                        }
                    }catch(e){}
                }
            })
        },
        loadDatad(data){
           this.$emit("loadDatad",data)
            
        },
        returns(){
                history.back();
            },
         searchEvent(data){
           
           this.searchStr2=JSON.parse(data);
            
        },
         rowDbClick(data){
                this.curRow=JSON.parse(data);
                this.curRow.rowIndex=this.index;
                this.$emit("rowDbClick",JSON.stringify(this.curRow));
            },
        rowClick(data){
            this.curRow=JSON.parse(data);
            if(this.curRow.rowIndex||this.curRow.rowIndex==0){
                this.curRowComplex[this.curRow.rowIndex]=JSON.parse(data);
            }
            this.$emit("rowClick",data);

            
        },
        rowMultiClick(data){
            let datas=JSON.parse(data);
            this.curRows=datas.row
            if(datas.rowIndex||datas.rowIndex==0){
                this.curRowsComplex[datas.rowIndex]=JSON.parse(JSON.stringify(datas.row));
            }
            this.$emit("rowMultiClick",data);
        },
        openWin(item){
            this.$parent.openWin(item);
        },
        goSearch(){
            let el=this.$refs.commonList;
            for(let i=0;i<el.length;i++){
                el[i].goSearch();
            }
        },
        initData(){
            let el=this.$refs.commonList;
            for(let i=0;i<el.length;i++){
                el[i].initData();
            }
           
        },
        getScrollTop(){
            let tops="";
            let el=this.$refs.commonList;
            for(let i=0;i<el.length;i++){
                let top= el[i].getScrollTop();
                tops=tops+top+","
            }
            tops=tops.substring(0,tops.length-1);
            return tops;
        },
        setScrollTop(top){
            let el=this.$refs.commonList;
            let tops=top.split(",");
            for(let i=0;i<tops.length;i++){
                if(el.length>i){
                    el[i].setScrollTop(tops[i])
                }
            }
        },
         getSearchData(){
            return this.$refs.tableList.getSearchData();
        },
        getSearchData2(){
            let data={};
            let dataEx={};
            for(let i=0;i<this.searchStr2.length;i++){
                let searchId=this.searchStr2[i].searchId;
                let val=this.searchStr2[i].val;
                if(this.searchStr2[i].vals){
                    val=this.searchStr2[i].vals;
                }
                data[searchId]=val;
                let _data={};
                _data.field=this.searchStr2[i].field;
                _data.searchType=this.searchStr2[i].searchType;
                _data.searchOper=this.searchStr2[i].searchOper;
                dataEx[searchId]=_data;

            }
            
            let _data_={}
            _data_.field=this.parentId;
            _data_.searchType="hidden";
            dataEx[this.parentId]=_data_;

            // data['tabStr']=JSON.stringify(this.tabJson);
            data.page=""+this.page;
            data.limit=""+this.limit;
            data.sKey=this.sKey;
            data.sKey2=this.sKey2;
            if(this.exId)
                data.exId=this.exId;
            else 
                data.exId="";
            data.listname=escape(this.listname);
            data.key=this.model_key;
            data['dataEx']=JSON.stringify(dataEx);
            data['data']=JSON.stringify(data);
            data['company']=this.company;
            data['model']=this.model;
            if(this.ordersList){
                let order="";
                for(let i=0;i<this.ordersList.length;i++){
                    let orderColumn=this.ordersList[i].orderColumn;
                    let orderState=this.ordersList[i].orderState;
                    orderState=orderState=="descending" ? "desc":orderState;
                    orderState=orderState=="ascending" ? "asc":orderState;

                    if(orderState){
                        order=order=="" ? orderColumn+" "+orderState:order+","+orderColumn+" "+orderState;
                    }
                }
                data["order"]=order;
            }
            
            return data;
        },
        btnClick(data){
                let item=JSON.parse(data);
                let wb_isEdit=item.wb_isEdit;
                let key=item.key;
                let isOpen=true;
                let wb_msg=item.wb_msg;
                let wb_frameindex=item.wb_frameindex;
                let sId="";
                let isOk=true;
                
                let filename=item.filename;
                let appid=item.appid;
                if(item.buttonName.indexOf("打印")!=-1){

                    let sId2="";
                    if(wb_frameindex||wb_frameindex=="0"){
                        if(this.curRowComplex[wb_frameindex]){
                            sId2=this.curRowComplex[wb_frameindex][key];
                        }
                    }else{
                        if(this.curRow){
                            sId2=this.curRow[key]
                        }
                    }
                    
                    let item_={};
                    item_.buttonName=item.buttonName;
                    let wb_msgs=wb_msg.split("#");
                    let isPrint=wb_msgs.length>1 ? wb_msgs[1]:"N";
                    item_.url="commonPrint?printName="+wb_msgs[0]+"&sCurItem="+sId2+"&appid="+appid+"&token="+sessionStorage.getItem("token")+"&isPrint="+isPrint;
                    
                    if(isPrint=="Y"){
                        let printData_={};
                        printData_.printName=wb_msgs[0];
                        printData_.sCurItem=sId2;
                        printData_.appid=appid;
                        printData_.token=sessionStorage.getItem("token");
                        printData_.isPrint=isPrint;
                        this.printData=printData_;
                        //document.getElementById("printIframe").src="";
                        //document.getElementById("printIframe").src="/#/"+item_.url;
                    }else{
                        item_.url="/"+item_.url;
                        this.$parent.openWin(JSON.stringify(item_));
                    }
                    isOk=false;
                } 
                if(isOk){
                    
                    if(wb_isEdit=="Y"&&this.curRow==null){
                        isOpen=false;
                        this.$message.error('请先选择一条数据');
                    }else if(wb_isEdit=="Y"||key){
                        //alert(wb_frameindex)
                        if(wb_frameindex||wb_frameindex=="0"){
                            if(this.curRowComplex[wb_frameindex]){
                                sId=this.curRowComplex[wb_frameindex][key];
                            }
                        }else{
                            if(this.curRow){
                                sId=this.curRow[key]
                            }
                        }
                        if(!sId)
                            sId="";
                        let url=item.url;
                        if(wb_msg=="")
                             url=url.indexOf("?")==-1 ? url+"?sId="+sId:url+"&sId="+sId;
                        item.url=url;
                        item.sId=sId;

                    }
                    if(wb_isEdit=="Y"&&!sId){
                        isOpen=false;
                        this.$message.error('请先选择一条数据');
                    }
                    
                   
                    if(isOpen&&wb_msg==""){
                        let url=item.url;
                        url=url.indexOf("?")==-1 ? url+"?appid="+appid:url+"&appid="+appid;
                        url=url+"&filename2="+filename;
                        url=url+"&key="+item.modelKey;
                        url=url+"&fieldPath="+item.field;
                        
                        url=url+"&model="+item.model;
                        
                        item.url=url;
                        item.appid=appid;
                        item.filename2=filename;
                        item.key=item.modelKey;
                        item.fieldPath=item.field;
                       
                        this.$parent.openWin(JSON.stringify(item));
                    }else if(wb_msg!=""){
                        
                        let url=item.url;
                        let ids="";
                        if(wb_frameindex||wb_frameindex=="0"){
                            if(this.curRowsComplex[wb_frameindex]){
                                for(let i=0;i<this.curRowsComplex[wb_frameindex].length;i++){
                                    let id=this.curRowsComplex[wb_frameindex][i][key];
                                    ids=ids=="" ? id:ids+","+id;
                                }
                            }else{
                                ids=this.curRowComplex[wb_frameindex][key];
                            }
                        }else{
                            if(this.curRows){
                                for(let i=0;i<this.curRows.length;i++){
                                    let id=this.curRows[i][key];
                                    ids=ids=="" ? id:ids+","+id;
                                }
                            }else{
                                ids=this.curRow[key];
                            }
                        }
                        
                        if(!ids){
                            this.$alert('请先选择一行数据', '错误提示', {
                                confirmButtonText: '确定',
                                callback: action => {}
                            });
                            return;
                        }
                         let params=null;
                        try{
                            params=this.getSearchData();
                        }catch(e){
                            params=this.getSearchData2();
                        }
                        this.$confirm(wb_msg, '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(() => {
                            params["ids"]=ids;
                            if(url.indexOf("filename")==-1){
                                params.filename=item.filename;
                            }
                            params["sCode"]=localStorage.getItem("sCode");
                            let urlss=url.split(",");
                            let urls=urlss[0].split("#");
                            let appid=item.appid;
                            if(urls.length>1){
                                appid=urls[0];
                                url=urls[1];

                            }
                            this.$post(appid,url,params).then(data=>{
                                if(data[0].status=="+OK"){
                                    if(urlss.length>1){
                                        let url_=urlss[1].split("#");
                                        this.$post(url_[0].replace("appid=",""),url_[1],params).then(data=>{
                                        });
                                    }
                                    this.$message(unescape(data[0].msg));
                                    this.goSearch()
                                }else{
                                    this.$alert(unescape(data[0].msg), '错误提示', {
                                        confirmButtonText: '确定',
                                        callback: action => {}
                                    });
                                }
                            });
                        }).catch(() => {
                                    
                        });
                    }
                }
                
                
               // this.$parent.$parent.postMessage(data,'*');
            }
    } 
}
</script> 
<style scoped>
    .commonListComplex{
        height:100%;
        width:100%;
        display: flex;
        flex-direction: column;

        overflow:hidden;
        box-sizing: border-box;
        -moz-box-sizing: border-box;
        -webkit-box-sizing: border-box;
        padding-top:0;
    } 
    .list{
        flex:1;
        box-sizing: border-box;
        -moz-box-sizing: border-box;
        -webkit-box-sizing: border-box;
        display:flex;
        flex-direction: column;
        overflow:hidden;
    }
    .listComplex_row{
        overflow:hidden;
        display:flex;
        flex:1;
        flex-direction: column;
    }
    .el-col{
        padding:5px 5px 5px 0;
        overflow: hidden;
    }
  
   .elrows2{
        background:#fff;    
        margin-top: 5px;
        border-radius: 5px;
    }
    .commonList_title{
        height:35px;
        border:1px solid #2294f2;
        padding-left:15px;
        border-bottom:0;
        line-height:35px;
        background:#2294f2;
        color:#fff;
    }
    .commonList_title i{
        margin-right:5px;
    }
   .commonList_title_center{
       text-align: center;
   }
   .commonList_title_center2{
    text-align:center;
    height:60px;
    padding-top:30px;
    box-sizing: border-box;
}
</style>